class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string, int>mp;
        for (auto x : words) {
            mp[x]++;
        }
        multimap<int, string, greater<int>>mmp;
        for (auto x : mp) {
            mmp.insert(make_pair(x.second, x.first));
        }
        vector<string>ans;
        for (auto x : mmp) {
            if (!k)break;
            ans.push_back(x.second);
            k--;
        }
        return ans;
    }
};